package com.sw.chinesewriteboard.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sw.chinesewriteboard.R;
import com.sw.chinesewriteboard.config.ChineseDataConfig;
import com.sw.chinesewriteboard.copybook.ZhCopybookActivity;
import com.sw.chinesewriteboard.model.ChineseData;
import com.sw.chinesewriteboard.operation.ChineseDataOperate;

import java.util.regex.PatternSyntaxException;

/**
 * @Description: 汉字字帖
 * @author: GordenGao
 * @Email: gordengao124@gmail.com
 * @date: 2021年06月21日 9:26
 */
public class ZhSymbolCopybookActivity extends ZhCopybookActivity {

    @Override
    protected void initOwnView() {
        showLoadingDialog();
//        setTitle("拼音字母笔顺帖");
        ChineseDataConfig.setRow(32);
        binding.vbnBottomNavigation.initEnvironment(this, true);
        binding.vbnBottomNavigation.setContantHint("请输入要生成字帖的拼音字母。例如：a b c");
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_rv).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_gezi_csl_kuandu).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_rv).setVisibility(View.GONE);
        binding.vbnBottomNavigation.findViewById(R.id.vbn_ziti_ll_zihao).setVisibility(View.GONE);
        binding.vbnBottomNavigation.removeTextWatcher();

        ((EditText) binding.vbnBottomNavigation.findViewById(R.id.vbn_nr_edit)).addTextChangedListener(new TextWatcher() {
            String str;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                EditText editText = binding.vbnBottomNavigation.findViewById(R.id.vbn_nr_edit);
                String str = editable.toString();
                String inputStr = stringFilterEn(str);
                editText.removeTextChangedListener(this);
                // et.setText方法可能会引起键盘变化,所以用editable.replace来显示内容
                editable.replace(0, editable.length(), inputStr);
                editText.addTextChangedListener(this);
            }
        });
    }

    public static String stringFilterEn(String str) throws PatternSyntaxException {
        // 只允许字母、和空格和汉字
        String regEx = "[a-zA-Z ]+";
        return str != null ? (str.matches(regEx) ? str : "") : "";
    }

    @Override
    protected boolean isPinyinEnabled() {
        return false;
    }

    @Override
    protected boolean needInitDir() {
        return false;
    }

    @Override
    protected ChineseDataOperate.ArrangeType setArrangeType() {
        return ChineseDataOperate.ArrangeType.SYMBOL;
    }

    public void refreshCopyBookView() {
        int COLUMN_LENGTH = ChineseDataConfig.TOTAL_LENGTH_COLUMN;
        int ROW_LENGTH = ChineseDataConfig.TOTAL_LENGTH_ROW;

        mWordPaper = new LinearLayout(this);
        mWordPaper.setLayoutParams(new LinearLayout.LayoutParams(ChineseDataConfig.getA4width(this), ChineseDataConfig.getA4height(this)));
        mWordPaper.setGravity(Gravity.CENTER);
        mWordPaper.setAlpha(0f);
        binding.getRoot().addView(mWordPaper);

        mWordList = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WRC, WRC);
        mWordList.setLayoutParams(layoutParams);
        mWordList.setOrientation(LinearLayout.VERTICAL);
        mWordList.removeAllViews();

        mEditTextList.clear();

        mData.setText(mChineseDataOperate.listToString(setArrangeType(), mListContent));
        mData.setTextSize(30);

        int startIndex = ChineseDataConfig.TOTAL_LENGTH * mPage;    // 字符串的起始坐标
        int k = startIndex;
        System.out.println("日志：p：" + mPage + "    k:" + k);
        for (int i = 0; i < ROW_LENGTH; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
            row.setOrientation(LinearLayout.HORIZONTAL);
            for (int j = 0; j < COLUMN_LENGTH; j++) {
                if (j == 0) {
                    mCharacterOperation.setIndex(mCharacterOperation.LEFTMOST);
                } else if (j == COLUMN_LENGTH - 1) {
                    mCharacterOperation.setIndex(mCharacterOperation.RIGHTMOST);
                } else {
                    mCharacterOperation.setIndex(mCharacterOperation.NORMAL);
                }
                View view;
                if (j == 0) {
                    view = createFirstLayout(mData.getText().toLowerCase().charAt(k), mData);
                } else {
                    view = createCharacterLayout(mData.getText().toLowerCase().charAt(k), mData);
                }
                view.setLayoutParams(new LinearLayout.LayoutParams(WRC, WRC));
                row.addView(view);
                k++;
            }
            System.out.println("GGGG" + row.getWidth());
            mWordList.addView(row);
        }
        mWordPaper.addView(mWordList);

        binding.cbIvCopybook.post(new Runnable() {
            @Override
            public void run() {
                binding.cbIvCopybook.setImageBitmap(loadBitmapFromView());
                binding.getRoot().removeView(mWordPaper);
                hideLoadingDialog();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    protected View createCharacterLayout(char c, ChineseData data) {
        int gridWidth = ChineseDataConfig.getGridWidth(ZhSymbolCopybookActivity.this);
        int gridHeight = gridWidth * 4 / 7;

        FrameLayout characterLayout = new FrameLayout(this);
        characterLayout.setLayoutParams(new LinearLayout.LayoutParams(gridWidth, gridHeight));

        final TextView pinyin = new TextView(this);
        pinyin.setGravity(Gravity.CENTER);
        pinyin.setEnabled(true);
        mCharacterOperation.set3LinesAppearance(pinyin, data);
        characterLayout.addView(pinyin, new LinearLayout.LayoutParams(gridWidth, gridHeight));

        final TextView text = new TextView(this);
        text.setText(c + "");
        text.setGravity(Gravity.CENTER);
        text.setTextSize(data.getTextSize());
        text.setTextColor(data.getFontColor().getColor());
        text.setTypeface(data.getMyTypeface().getTypeface());
        text.setAlpha(data.getAlpha());
        characterLayout.addView(text, new LinearLayout.LayoutParams(gridWidth, gridHeight));

        return characterLayout;
    }

    @SuppressLint("SetTextI18n")
    protected View createFirstLayout(char c, ChineseData data) {
        int gridWidth = ChineseDataConfig.getGridWidth(ZhSymbolCopybookActivity.this);
        int gridHeight = gridWidth * 4 / 7;

        FrameLayout characterLayout = new FrameLayout(this);
        characterLayout.setLayoutParams(new FrameLayout.LayoutParams(gridWidth, gridHeight));

        final TextView pinyin = new TextView(this);
        pinyin.setGravity(Gravity.CENTER);
        pinyin.setEnabled(true);

        characterLayout.addView(pinyin, new LinearLayout.LayoutParams(gridWidth, gridHeight));

        mCharacterOperation.set3LinesAppearance(pinyin, data);

        if (c != ' ') {
            ImageView symbol = new ImageView(this);
            symbol.setBackgroundResource(mChineseDataOperate.getSymbol(c + ""));
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(gridHeight, gridHeight);
            layoutParams.gravity = Gravity.CENTER;
            symbol.setPadding(0, 0, 0, 10);
            characterLayout.addView(symbol, layoutParams);
        }

        return characterLayout;
    }
}
